﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AF_Web
{
    public static class AshxHelper
    {
        public static string GetRequestString(this HttpContext context, string key, out bool hasValue)
        {
            hasValue = false;
            string result = "";
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = context.Server.UrlDecode(str);
                hasValue = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = context.Server.UrlDecode(str);
                hasValue = true;
            }
            return result;
        }

        public static int GetRequestInt(this HttpContext context, string key, out bool hasValue)
        {
            hasValue = false;
            int result = -1;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                int.TryParse(str, out result);
                hasValue = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                int.TryParse(str, out result);
                hasValue = true;
            }
            return result;
        }

        public static decimal GetRequestDecimal(this HttpContext context, string key, out bool hasValue)
        {
            hasValue = false;
            decimal result = -1;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                decimal.TryParse(str, out result);
                hasValue = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                decimal.TryParse(str, out result);
                hasValue = true;
            }
            return result;
        }

        public static Guid GetRequestGuid(this HttpContext context, string key, out bool hasValue)
        {
            hasValue = false;
            Guid result = Guid.Empty;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                Guid.TryParse(str, out result);
                hasValue = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                Guid.TryParse(str, out result);
                hasValue = true;
            }
            return result;
        }

        public static bool GetRequestBoolean(this HttpContext context, string key, out bool hasValue)
        {
            hasValue = false;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                bool.TryParse(str, out result);
                hasValue = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                bool.TryParse(str, out result);
                hasValue = true;
            }
            return result;
        }

        public static DateTime GetRequestDateTime(this HttpContext context, string key, out bool hasValue)
        {
            hasValue = false;
            DateTime result = DateTime.MinValue;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                DateTime.TryParse(str, out result);
                hasValue = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                DateTime.TryParse(str, out result);
                hasValue = true;
            }
            return result;
        }

    }
}